
--使用方法：直接粘贴到模组环境 "abc"改成角色标签

local FIRE_MUST_TAGS = {"character"}
local NO_TAGS = { "playerghost", "ghost", "shadow", "shadowminion", "noauradamage", "FX", "INLIMBO", "notarget", "noattack", "flight", "invisible" }
local FIRE_RADIUS = 5
local function OnUpdate(inst)
	local temp = inst.watchers_toremove
	inst.watchers_toremove = inst.watchers_exiting
	inst.watchers_exiting = inst.watchers
	inst.watchers = temp

	local x, y, z = inst.Transform:GetWorldPosition()

    local miasmamanager = TheWorld.components.miasmamanager
    local nearestfire

    for k,v in pairs(TheSim:FindEntities(x, y, z, FIRE_RADIUS, FIRE_MUST_TAGS)) do
        if v:HasTag("abc") then
            nearestfire = true
            break
        end
    end
    
    if nearestfire then
        if miasmamanager and miasmamanager:GetMiasmaAtPoint(x, y, z) then
            miasmamanager:SetMiasmaDiminishingAtPoint(x, y, z, true)
        else
            -- Fake diminishing an unmanaged miasma cloud.
            inst._miasma_kill_task = inst:DoTaskInTime(.5, inst.Remove)
        end
        inst._diminishing:set(true)
        inst:StopAllWatchers()
    end

	inst:ClearWatcherTable(inst.watchers_toremove)
end

AddPrefabPostInit("miasma_cloud", function(inst)
    if not TheWorld.ismastersim then
        return
    end

    local _StartAllWatchers = inst.StartAllWatchers
    inst.StartAllWatchers = function(inst, ...)
        if inst.i23task == nil then
            inst.i23task = inst:DoPeriodicTask(1, OnUpdate, math.random() * .5)
        end
        return _StartAllWatchers(inst, ...)
    end

    local _StopAllWatchers = inst.StopAllWatchers
    inst.StopAllWatchers = function(inst, ...)
        if inst.i23task ~= nil then
            inst.i23task:Cancel()
            inst.i23task = nil
        end
        return _StopAllWatchers(inst, ...)
    end
end)